//
//  MBDModifyInfoViewController.swift
//  MasterBoy
//
//  Created by kyson on 2023/3/28.
//

import Foundation

import UIKit
import YYText
import SnapKit
import Toast_Swift
import SDCycleScrollView
import RxSwift
import RxCocoa

typealias KButtonBlock = (_ str :String, _ eType : MBDModyfiyType) ->()

// 定义枚举
enum MBDModyfiyType {
    case textField
    case textView
}

class MBDModifyInfoTextViewEditCellModel : MBDModifyInfoBaseCellModel {

    override func cellHeight() -> CGFloat {
        return 210
    }
    
}

class MBDModifyInfoTextFieldEditCellModel: MBDModifyInfoBaseCellModel {

    override func cellHeight() -> CGFloat {
        return 90
    }
}

class MBDModifyInfoBaseCellModel: MBDCommonCellModel {
    var text : String?
    var textLimit : Int?

    var cellWillDisplayBlock : (()-> Void)?

}


class MBDModifyInfoViewModel : NSObject {
    var cellModels : Array<Any>!
    open var editType : MBDModyfiyType
    open var placeHolder : String
    open var title : String

    init(editType: MBDModyfiyType = .textField, placeHolder: String = "", title: String = "") {
        self.editType = editType
        self.placeHolder = placeHolder
        self.title = title
        self.editType = editType

        super.init()
        
        var cellModels = [Any]()

        if self.editType == .textView {

            let cellModel0 = MBDModifyInfoTextViewEditCellModel()
            cellModel0.textLimit = 10000
            cellModel0.text = placeHolder
            cellModels.append(cellModel0)
            
        } else {
            let cellModel0 = MBDModifyInfoTextFieldEditCellModel()
            cellModel0.textLimit = 30
            cellModel0.text = placeHolder
            cellModels.append(cellModel0)
        }
                
        self.cellModels = cellModels
    }
    
}

class MBDModifyInfoViewController : MBDBaseViewController {
    
    var tableView: UITableView!
    var saveInfoBlock: KButtonBlock!
    var galleryView : SDCycleScrollView!
    var viewModel : MBDModifyInfoViewModel!
    
    init(viewModel : MBDModifyInfoViewModel) {
        super.init(nibName: nil, bundle: nil)
        self.viewModel = viewModel
    }
    
    public required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.viewModel = nil
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = self.viewModel.title
        
        let rightItem = UIButton(frame: CGRectMake(0, 0, 44, 44))
        rightItem.setTitle("保存", for: .normal)
        rightItem.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        rightItem.addTarget(self, action: #selector(rightItemPressed), for: .touchUpInside)
        rightItem.setTitleColor(UIColor(colorHex: 0x4273d0), for: .normal)
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: rightItem)
        
        tableView = UITableView()
        tableView.translatesAutoresizingMaskIntoConstraints = false
        self.view.addSubview(tableView)
        
        self.tableView.snp.makeConstraints { make in
            make.top.equalTo(self.view)
            make.left.equalTo(self.view)
            make.right.equalTo(self.view)
            make.height.equalTo(self.view)
        }
        
        tableView.backgroundColor = UIColor(colorHex: 0xF4F4F4)
        tableView.contentInset = UIEdgeInsets(top: 5, left: 0, bottom: 0, right: 0)
        tableView.showsVerticalScrollIndicator = false
        tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
        
        tableView.dataSource = self
        tableView.delegate = self
        tableView.estimatedRowHeight = 100

        tableView.register(MBDTextEditCell.self, forCellReuseIdentifier: "MBDTextEditCell")
        tableView.register(MBDTextViewEditCell.self, forCellReuseIdentifier: "MBDTextViewEditCell")
        
        self.view.setDynamicBackgroundColor()
        tableView.setDynamicBackgroundColor()
    }
    
    @objc func rightItemPressed(){
        let cellModel = self.viewModel.cellModels.first as! MBDModifyInfoBaseCellModel
        
        if saveInfoBlock != nil {
            saveInfoBlock(cellModel.text!,self.viewModel.editType )
        }
        self.navigationController?.popViewController(animated: true)
    }
    
    override func updateColors() {
        self.view.setDynamicBackgroundColor()
        tableView.setDynamicBackgroundColor()
        self.tableView.reloadData()
    }
    
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .default
    }
    
    override var prefersStatusBarHidden: Bool {
        return false
    }
    
}

extension MBDModifyInfoViewController: UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.viewModel.cellModels.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cellModel = self.viewModel.cellModels[indexPath.row]
        let cell : UITableViewCell?
        if cellModel is MBDModifyInfoTextViewEditCellModel {
            cell = tableView.dequeueReusableCell(withIdentifier: "MBDTextViewEditCell", for: indexPath)
        } else if cellModel is MBDModifyInfoTextFieldEditCellModel {
            cell = tableView.dequeueReusableCell(withIdentifier: "MBDTextEditCell", for: indexPath)
        } else {
            cell = UITableViewCell()
        }
        (cell as! MBDCommonCell).cellModel = cellModel
        return cell ?? UITableViewCell()
    }
}


extension MBDModifyInfoViewController : UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let cellModel = self.viewModel.cellModels[indexPath.row] as! MBDCommonCellModel
        return cellModel.cellHeight()
    }
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        let cellModel = self.viewModel.cellModels[indexPath.row] as! MBDModifyInfoBaseCellModel
        if let realBlock = cellModel.cellWillDisplayBlock {
            realBlock()
        }
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        UIApplication.shared.sendAction(#selector(resignFirstResponder), to: nil, from: nil, for: nil)
    }
}

@MainActor protocol MBDModifyInfoViewControllerDelegate : NSObjectProtocol {
    func saveButtonDidClicked(title:String,editType:MBDModyfiyType)
}
